#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "sdlstart.h"
#include <string>

using namespace std;

sdlstart::sdlstart()
{

}

SDL_Surface *sdlstart::load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        //Free the old image
        SDL_FreeSurface( loadedImage );

        //If the image was optimized just fine
        if( optimizedImage != NULL )
        {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );

            //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }
    }

    //Return the optimized image
    return optimizedImage;
}

void sdlstart::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Temporary rectangle to hold the offsets
    SDL_Rect offset;
    //Get the offsets
    offset.x = x;
    offset.y = y;
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

bool sdlstart::init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Screen attributes
    SCREEN_WIDTH = 800;
    SCREEN_HEIGHT = 600;
    SCREEN_BPP = 32;

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Game", NULL );

    //If everything initialized fine
    return true;
}

bool sdlstart::load_files()
{
    //Load the background image
    background = load_image( "dungeon.png" );

    //If there was an error in loading the image
    if( background == NULL )
    {
        return false;
    }

    //Load player image
    char1 = load_image( "Gruff2.png" );

    //If there was an error in loading the image
    if( char1 == NULL )
    {
        return false;
    }

    //Load player image
    char2 = load_image( "Wiz2.png" );

    //If there was an error in loading the image
    if( char2 == NULL )
    {
        return false;
    }

    //Load player image
    char3 = load_image( "Ehren2.png" );

    //If there was an error in loading the image
    if( char3 == NULL )
    {
        return false;
    }


    //Load enemy image
    enemy = load_image( "demon2.png" );

    //If there was an error in loading the image
    if( enemy == NULL )
    {
        return false;
    }

    //Load enemy image
    enemy2 = load_image( "demon.png" );

    //If there was an error in loading the image
    if( enemy2 == NULL )
    {
        return false;
    }

    //Load enemy image
    hud = load_image( "hud.png" );

    //If there was an error in loading the image
    if( hud == NULL )
    {
        return false;
    }

        //Load enemy image
    enemy2 = load_image( "demon.png" );

    //If there was an error in loading the image
    if( enemy2 == NULL )
    {
        return false;
    }
        //Load enemy image
    char1healthbar = load_image( "redbar.png" );

    //If there was an error in loading the image
    if( char1healthbar == NULL )
    {
        return false;
    }
        //Load enemy image
    char2healthbar = load_image( "greenbar.png" );

    //If there was an error in loading the image
    if( char2healthbar == NULL )
    {
        return false;
    }
        //Load enemy image
    char3healthbar = load_image( "bluebar.png" );

    //If there was an error in loading the image
    if( char3healthbar == NULL )
    {
        return false;
    }


    //Hud Images
    Gruff_Hud = load_image( "GruffHud.png" );

    //If there was an error in loading the image
    if( Gruff_Hud == NULL )
    {
        return false;
    }

    Wiz_Hud = load_image( "WizHud.png" );

    //If there was an error in loading the image
    if( Wiz_Hud == NULL )
    {
        return false;
    }

    Ehron_Hud = load_image( "EhronHud.png" );

    //If there was an error in loading the image
    if( Ehron_Hud == NULL )
    {
        return false;
    }


    //If everything loaded fine
    return true;
}

void sdlstart::clean_up()
{
    //Free the images
    SDL_FreeSurface( background );
    SDL_FreeSurface( char1 );
    SDL_FreeSurface( char2 );
    SDL_FreeSurface( char3 );
    SDL_FreeSurface( enemy );
    SDL_FreeSurface( hud );

    //Quit SDL
    SDL_Quit();
}
